<?php


namespace App\Logic;

use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Cache;
use App\Mail\Register;

class MailLogic
{

    /**
     * 发送注册邮件
     * @param $email
     * @return bool|string
     */
    public function register($email)
    {

        $code = mt_rand(100000, 999999);
        try {
            Mail::to(['email' => $email])
                ->queue(new Register($code));

            // 设置缓存 600秒过期
            Cache::put($email, $code, 600);
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        return true;
    }

    /**
     * 验证邮箱注册码
     * @param $email
     * @param $code
     * @return bool
     */
    public function checkRegister($email, $code)
    {
        $getCode = Cache::get($email);
        if (empty($getCode) || empty($code)) {
            return false;
        }
        return $getCode == $code;
    }

}
